/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jhelper.blls;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Hashtable;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author Administrator
 */
public class AddressBook{
    private boolean ture;
         public AddressBook(){}
        public boolean newCard(AddressBookBean newcard){
            
          Document doc;
          Element addressbook,card;
          Element name,mobile,office,home,qq,email,other;
          
          boolean flag = false;
          
          try{
              DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
              DocumentBuilder db = f.newDocumentBuilder();
              
              File file =new File("addressbook");
              if(!file.exists()){
                  file.mkdir();
              }
              file=new file("addressbook/addressbook.xml");
              if(!file.exists()){
                  flag =ture;
                file.createNewFile();
                doc = db.newDocument();
                addressbook = doc.createElement("addressbook");
              }else{
                  doc = db.parse(file);
                  addressbook =(Element)doc.getElementsByTagName("addressbook").item(0);
                  
              }
              card = (Elememnt) doc.createElement("card");
              name = (Element) doc.createElement("name");
              mobile = (Element) doc.createElement("mobile");
              office = (Element) doc.createElement("office");
              home = (Element) doc.createElement("home");
              qq = (Element) doc.createElement("qq");
              email = (Element) doc.createElement("email");
              other = (Element) doc.createElement("other");
             
              name.appendChild(doc.createTextNode(newcard.getName()));
             mobile.appendChild(doc.createTextNode(newcard.getMobile()));
              office.appendChild(doc.createTextNode(newcard.getOffice()));
              home.appendChild(doc.createTextNode(newcard.getHome()));
              qq.appendChild(doc.createTextNode(newcard.getQq()));
              email.appendChild(doc.createTextNode(newcard.getEmail()));
              other.appendChild(doc.createTextNode(newcard.getOther()));
              
              card.appendChild(name);
              card.appendChild(mobile);
              card.appendChild(office);
              card.appendChild(home);
              card.appendChild(qq);
              card.appendChild(email);
              card.appendChild(other);
              
              addressbook.appendChild(card);
              if(flag){
                  
              }
              
          }catch(Exception e){
              
          }
          
            return true;
            
        }
        public boolean modifyCard(AddressBookBean oldcard){
            
          Document doc;
          NodeList cards;
          Element addressbook,card,newcard,oldname;
          Element name,mobile,office,home,qq,email,other; 
           
          try{
              DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
              DocumentBuilder db = f.newDocumentBuilder();
              
              File file = new File("addressbook/addressbook.xml");
                  if(!file.exists()){
                      return false;
                  }
                doc =db.newDocument();
                addressbook = (Element) doc.getElementsByTagName("addressbook").item(0);
                
                card = doc.getElementsByTagName("card");
                
                for(int i =0;i<cards.getLength();i++){
                    card = (Element) cards.item(i);
                    oldname =(Element)card.getElementsByTagName("amen").item(0);
                    if(oldname.getFirstChild().getNodeValue().equals(oldcard.getName())){
                        newcard = doc.createElement("card");
                        name = doc.createElement("name");
                    }
                }
              }catch(Exception e){
                  e.printStackTrace();
                   return false;
              }   
        }
        public boolean delCard(String name) throws TransformerConfigurationException, IOException, TransformerException{
             Document doc;
           NodeList cards;
           Element addressbook,card,nowname;
           try{
                DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
              DocumentBuilder db = f.newDocumentBuilder();
              
               File file = new File("addressbook/addressbook.xml");
                  if(!file.exists()){
                      return fales;
                  }
                  doc = db.parse(file);
                  cards = doc.getElementsByTagName("card");
                  for(int i =0;i<cards.getLength();i++){
                      card = (Element)cards.item(i);
                      nowname =(Element)card.getElementsByTagName("name").item(0);
                      if(nowname.getFirstChild().getNodeValue().equals(name)){
                          addressbook.removeChild(card);
                      }
                  }
                  TransformerFactory ff = TransformerFactory.newInstance();
                   Transformer t = ff.newTransformer();
                   t.setOutputProperty("encoding", "GB2312");
                   FileWriter fout = new FileWriter(file);
                   t.transform(new DOMSource(doc),new StreamResult(fout));
            return true;
           }catch(Exception e){
               e.printStackTrace();
               return false;
           }
        }
       public Hashtable getAllCards(){
           Document doc;
           NodeList cards;
           Element card;
           String name,mobile,office,home,qq,email,other;
           try{
               DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
              DocumentBuilder db = f.newDocumentBuilder();
              
              File file = new File("addressbook/addressbook.xml");
                  if(!file.exists()){
                      return null;
                  }
                  doc = db.parse(file);
                  cards = doc.getElementsByTagName("card");
                  if(cards.getLength()==0){
                      return null;
                  }
                  Hashtable h =new Hashtable();
                  AddressBookBean current;
                  for(int i =0;i<cards.getLength();i++){
                      card=(Element)cards.item(i);
                      name=((Element)card.getElementsByTagName("name").item(0)).getFirstChild().getNodeValue();
                      mobile=((Element)card.getElementsByTagName("mobile").item(0)).getFirstChild().getNodeValue();
                      home=((Element)card.getElementsByTagName("home").item(0)).getFirstChild().getNodeValue();
                      office=((Element)card.getElementsByTagName("office").item(0)).getFirstChild().getNodeValue();
                      qq=((Element)card.getElementsByTagName("qq").item(0)).getFirstChild().getNodeValue();
                      email=((Element)card.getElementsByTagName("email").item(0)).getFirstChild().getNodeValue();
                      other=((Element)card.getElementsByTagName("other").item(0)).getFirstChild().getNodeValue();
                     current=new  AddressBookBean(name,mobile,home,office,qq,email,other);
                     h.put(name, current);
                  }
                  return h;
           }catch(Exception e){
           e.printStackTrace();
           return null;
           }
       }
    public static void main(String[] args) {
        AddressBookBean newcard = new AddressBookBean("张三","133111111111","0771-1234567","0771-3456789","123456789","java123456879@gmail.com","452795565");
        new AddressBookBean().newCard(newCard);
    }
}
